package com.hzeport.bgs22emduty.inbound.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.entity.SystemModule;
import com.hzeport.bgs22emduty.outbound.SystemModuleRepository;
import com.hzeport.bgs22emduty.vo.SystemModule.SystemModuleVO;
import com.hzeport.bgs22emduty.auth.adapter.UserService;
import com.hzeport.bgs22emduty.auth.dto.Function;
import com.hzeport.bgs22emduty.auth.dto.Role;
import com.hzeport.bgs22emduty.auth.dto.User;
import com.hzeport.bgs22emduty.auth.h4a.constant.H4AEnum;
import com.hzeport.bgs22emduty.auth.porperty.SystemProperties;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zwj
 * @desc H4Aq权限菜单
 * @date 2021-11-11 17:00
 */
@Slf4j
@RestController
@RequestMapping("/H4AUser")
@Api(value = "H4AUser", tags = "H4A用户接口")
@AllArgsConstructor
@Profile("bgs22emduty")
public class H4AUserTestController {
    public final UserService userService;

    public final SystemProperties systemProperties;
    public final LoginCompoentService loginCompoentService;
    @Autowired
    private SystemModuleRepository systemModuleRepository;
    /**
     * @param request
     * @return
     * @Authentication(roles = "FORUM_ADMIN") 权限校验
     */
//    @Authentication(roles = "MANAGER")
    @ApiOperation(value = "获取用户信息")
//    @ApiImplicitParam(name = "ticket", value = "token", required = true, dataType = "string", paramType = "header")
    @GetMapping("/userInfo")
    public BaseResult<UserInfoDetailVO> getUserInfo(HttpServletRequest request) {
//        log.info("进入获取用户信息");
//        String ticket = request.getHeader(H4AEnum.HEAD_TICKET.getValue());
//        log.info(ticket);
//        User user = userService.getUsers(ticket);
        UserInfoDetailVO user= JSONObject.parseObject("{\n" +
                "\t\"hrcode\": \"2905230\",\n" +
                "\t\"personId\": \"2905230\",\n" +
                "\t\"userId\": \"2905230\",\n" +
                "\t\"name\": \"朱某某\",\n" +
                "\t\"userName\": \"朱某某\",\n" +
//                "\t\"customscode\": \"ff27946f-5536-46e2-8c17-e09e6adaa209\",\n" +
//                "\t\"customsName\": \"重庆海关\",\n" +
                "\t\"customscode\": \"94480300-fca2-4268-86c2-4c13963bf4cb\",\n" +
                "\t\"customsName\": \"海关总署\",\n" +
//                "\t\"rankName\": \"副主任科员\",\n" +
//                "\t\"fullPath\": \"海关总署\\\\杭州海关\\\\办公室\\\\秘书科\\\\周某某\",\n" +
//                "\t\"parentId\": \"ff27946f-5536-46e2-8c17-e09e6adaa20\",\n" +
//                "\t\"organization\": \"办公室,秘书科\",\n" +
                "\t\"sideLine\": true,\n" +
//                "\t\"role\": ['ZGGLY'],\n" +
                "\t\"role\": ['ZGGLY'],\n" +
                "\t\"entract\": null\n" +
                "}", UserInfoDetailVO.class);
// 总署
//        UserInfoDetailVO user= JSONObject.parseObject("{\n" +
//                "\t\"hrcode\": \"2905230\",\n" +
//                "\t\"personId\": \"2905230\",\n" +
//                "\t\"userId\": \"2905230\",\n" +
//                "\t\"name\": \"朱某某\",\n" +
//                "\t\"userName\": \"朱某某\",\n" +
//                "\t\"customscode\": \"ff27946f-5536-46e2-8c17-e09e6adaa209\",\n" +
//                "\t\"customsName\": \"重庆海关\",\n" +
//                //                "\t\"customscode\": \"b017dbfe-e9a5-4ba2-86e0-fb543372c882\",\n" +
//                //                "\t\"customsName\": \"广东分署\",\n" +
//                //                "\t\"rankName\": \"副主任科员\",\n" +
//                //                "\t\"fullPath\": \"海关总署\\\\杭州海关\\\\办公室\\\\秘书科\\\\周某某\",\n" +
//                //                "\t\"parentId\": \"ff27946f-5536-46e2-8c17-e09e6adaa20\",\n" +
//                //                "\t\"organization\": \"办公室,秘书科\",\n" +
//                "\t\"sideLine\": true,\n" +
//                //                "\t\"role\": ['ZGGLY'],\n" +
//                "\t\"role\": ['ZSGGLY'],\n" +
//                "\t\"entract\": null\n" +
//                "}", UserInfoDetailVO.class);

        return BaseResult.ok(user);
    }

//    @Authentication(roles = "MANAGER")
    @ApiOperation(value = "获取用户角色")
//    @ApiImplicitParam(name = "ticket", value = "token", required = true, dataType = "string", paramType = "header")
    @GetMapping("/getRoles")
    public BaseResult<List<Role>> getRoles(HttpServletRequest request) {
//        log.info("进入获取用户角色");
//        String ticket = request.getHeader(H4AEnum.HEAD_TICKET.getValue());
//        log.info(ticket);
//        List<Role> roles = userService.getRoles(ticket);
        Role role=new Role();
        role.setCode_name("MANAGER");
//        role.setCode_name("CJCONTACT");
        List<Role> roles= Arrays.asList(role);
        return BaseResult.ok(roles);
    }

//    @Authentication(roles = "MANAGER")
    @ApiOperation(value = "获取用户菜单")
//    @ApiImplicitParam(name = "ticket", value = "token", required = true, dataType = "string", paramType = "header")
    @GetMapping("/menuInfo")
    public BaseResult<List<SystemModuleVO>> getMenuInfo(HttpServletRequest request) {
        log.info("进入获取用户菜单");
        String ticket = request.getHeader(H4AEnum.HEAD_TICKET.getValue());
        log.info(ticket);
        List<String> functionCodes=JSONArray.parseArray("[\n" +
                "\t\"FUNC_DUTY_1000\",\n" +
                "\t\"FUNC_DUTY_1002\",\n" +
                "\t\"FUNC_DUTY_1003\",\n" +
                "\t\"FUNC_DUTY_1004\",\n" +
                "\t\"FUNC_DUTY_1005\",\n" +
                "\t\"FUNC_DUTY_2000\",\n" +
                "\t\"FUNC_DUTY_2005\",\n" +
                "\t\"FUNC_DUTY_2009\",\n" +
                "\t\"FUNC_DUTY_2008\",\n" +
                "\t\"FUNC_DUTY_2002\",\n" +
                "\t\"FUNC_DUTY_2006\",\n" +
                "\t\"FUNC_DUTY_2004\",\n" +
                "\t\"FUNC_DUTY_2010\",\n" +
                "\t\"FUNC_DUTY_3000\",\n" +
                "\t\"FUNC_DUTY_3001\",\n" +
                "\t\"FUNC_DUTY_3002\",\n" +
                "\t\"FUNC_DUTY_3003\",\n" +
                "\t\"FUNC_DUTY_3004\",\n" +
                "\t\"FUNC_DUTY_3005\",\n" +
                 "\t\"FUNC_DUTY_4000\",\n" +
                "\t\"FUNC_DUTY_4001\",\n" +
                "\t\"FUNC_DUTY_4002\",\n" +
                "\t\"FUNC_DUTY_4003\",\n" +
                "\t\"FUNC_DUTY_4004\",\n" +
                "\t\"FUNC_DUTY_4005\",\n" +
                "\t\"FUNC_DUTY_4006\",\n" +
                "\t\"FUNC_DUTY_4007\"" +
                "\t\"FUNC_DUTY_4008\",\n" +
                "\t\"FUNC_DUTY_4009\",\n" +

                "\t\"FUNC_DUTY_4010\",\n" +
                "]",String.class);
//        List<String> functionCodes=JSONArray.parseArray("[\n" +
//                        "\t\"FUNC_DUTY_1000\",\n" +
//                        "\t\"FUNC_DUTY_1001\",\n" +
//                        "\t\"FUNC_DUTY_2000\",\n" +
//                        "\t\"FUNC_DUTY_2001\",\n" +
//                        "\t\"FUNC_DUTY_2002\",\n" +
//                        "\t\"FUNC_DUTY_2003\",\n" +
//                        "\t\"FUNC_DUTY_2004\",\n" +
//                        "\t\"FUNC_DUTY_2005\",\n" +
//                        "\t\"FUNC_DUTY_2006\",\n" +
//                        "\t\"FUNC_DUTY_2007\",\n" +
//                        "\t\"FUNC_DUTY_3005\",\n" +
//                        "\t\"FUNC_DUTY_3000\",\n" +
//                        "\t\"FUNC_DUTY_3001\",\n" +
//                        "\t\"FUNC_DUTY_3002\",\n" +
//                        "\t\"FUNC_DUTY_3005\",\n" +
//                        "\t\"FUNC_DUTY_4000\",\n" +
//                        "\t\"FUNC_DUTY_4006\",\n" +
//                        "\t\"FUNC_DUTY_4008\",\n" +
//                        "\t\"FUNC_DUTY_3003\"" +
//                        "]",String.class);
//         List<String> functionCodes=JSONArray.parseArray("[\n" +
//                        "\t\"FUNC_DUTY_1000\",\n" +
//                        "\t\"FUNC_DUTY_1001\",\n" +
//                 "\t\"FUNC_DUTY_1004\",\n" +
//                 "\t\"FUNC_DUTY_2000\",\n" +
//                        "\t\"FUNC_DUTY_2001\",\n" +
//                        "\t\"FUNC_DUTY_2002\",\n" +
//                        "\t\"FUNC_DUTY_2003\",\n" +
//                        "\t\"FUNC_DUTY_2004\",\n" +
//                        "\t\"FUNC_DUTY_2005\",\n" +
//                        "\t\"FUNC_DUTY_2006\",\n" +
//                        "\t\"FUNC_DUTY_2007\",\n" +
//                        "\t\"FUNC_DUTY_3005\",\n" +
//                        "\t\"FUNC_DUTY_3000\",\n" +
//                        "\t\"FUNC_DUTY_3001\",\n" +
//                        "\t\"FUNC_DUTY_3002\",\n" +
//                        "\t\"FUNC_DUTY_3005\",\n" +
//                        "\t\"FUNC_DUTY_4000\",\n" +
//                        "\t\"FUNC_DUTY_4006\",\n" +
//                        "\t\"FUNC_DUTY_4008\",\n" +
//                 "\t\"FUNC_DUTY_4011\",\n" +
//                 "\t\"FUNC_DUTY_4012\",\n" +
//                 "\t\"FUNC_DUTY_4013\",\n" +
//                 "\t\"FUNC_DUTY_4014\",\n" +
//                 "\t\"FUNC_DUTY_1005\",\n" +
//                        "\t\"FUNC_DUTY_3003\"" +
//                        "]",String.class);

        //菜单树集合
//        functionCodes.add("10000");
//        functionCodes.add("11000");
        List<SystemModuleVO> systemModuleVOList = new ArrayList<>();
//        全部菜单
        List<SystemModule> List = systemModuleRepository.list();
        //菜单树为空
        if(!CollectionUtils.isEmpty(functionCodes)){
            //在全部菜单中找到每个人对应功能点菜单
            List<SystemModule> moduleList=new ArrayList<>();
            for (String functionCode : functionCodes) {
                moduleList.addAll(List.stream().filter(i -> i.getFauthorityid().equals(functionCode)).collect(Collectors.toList()));
            }
            //菜单排序
            List<SystemModule> moduleLists=moduleList.stream().sorted(Comparator.comparing(SystemModule::getForderindex)).collect(Collectors.toList());
            //        找到一级菜单
        for (SystemModule systemModule : moduleLists) {
                if(systemModule.getMLevel()==1 ){
                    //根据一级菜单找到二级
                    List<SystemModuleVO> Children=new ArrayList<>();  //二级菜单集合
                    getChild(systemModule,Children,moduleLists);//递归找到二级菜单
                    //二级菜单排序
                    List<SystemModuleVO> Childrens=Children.stream().sorted(Comparator.comparing(SystemModuleVO::getForderindex)).collect(Collectors.toList());
                    SystemModuleVO systemModuleVO = new SystemModuleVO();//菜单树对象
                    systemModuleVO.setId(systemModule.getId());
                    systemModuleVO.setLabel(systemModule.getFmainalias());
                    systemModuleVO.setPath(systemModule.getWPath());
                    systemModuleVO.setChildren(Childrens);
                    //添加到集合
                    systemModuleVOList.add(systemModuleVO);
                }
            }
        }else {

        }

        return BaseResult.ok(systemModuleVOList);
    }
    private void  getChild(SystemModule systemModule, List<SystemModuleVO> Children , List<SystemModule> moduleList ){
        List<SystemModule> myChildren=moduleList.stream().filter(i->i.getFsuperid()!=null&&i.getFsuperid().toString().equals(systemModule.getId())).collect(Collectors.toList());
        List<SystemModuleVO> myChildrenVO=new ArrayList<>();
        for (SystemModule myChild : myChildren) {
            SystemModuleVO systemModuleVO = new SystemModuleVO();
            systemModuleVO.setId(myChild.getId());
            systemModuleVO.setLabel(myChild.getFmainalias());
            systemModuleVO.setPath(myChild.getWPath());
            systemModuleVO.setForderindex(myChild.getForderindex());
            myChildrenVO.add(systemModuleVO);
        }
        if (!CollectionUtils.isEmpty(myChildren)){
            Children.addAll(myChildrenVO);
            for (SystemModule child:myChildren){
                getChild(child,Children,moduleList);
            }
        }else {
            return ;
        }
    }

    @ApiOperation(value = "菜单", notes = "菜单")
    @GetMapping("/systemModule")
    public BaseResult systemModule()  {
        return BaseResult.ok(loginCompoentService.systemModule());
    }


    @ApiOperation(value = "获取关区列表", notes = "获取关区列表")
    @GetMapping("/getCustomCodes")
    public BaseResult getCustomCodes ()  {
        return BaseResult.ok(loginCompoentService.getCustomCodes());
    }


    @ApiOperation(value = "选择关区", notes = "获取关区列表")
    @PostMapping("/setCustom")
    public BaseResult getCustomCodes ( @RequestParam("customCode")String customCode )  {
        return BaseResult.ok(loginCompoentService.setCustom(customCode));
    }
}
